package io.stream;

import java.io.*;

public class Testreaderwriter {

    public  static  void  main(String []args) throws IOException {

        //copyAndWrite1();
        //copyAndWrite2();
        //copyAndWrite3();
        //copyAndWrite4();
        copyAndWrite5();
    }

    private static void copyAndWrite1() throws IOException {
        InputStreamReader inputStreamReader=new InputStreamReader(new  FileInputStream("rs/a.txt"));
        OutputStreamWriter outputStreamWriter =new OutputStreamWriter(new FileOutputStream("c.txt"));
        int byt=0;
        while ((byt=inputStreamReader.read())!=-1)
        {
            System.out.print((char)byt);
            outputStreamWriter.write(byt);
        }
        outputStreamWriter.close();
        inputStreamReader.close();
    }

    private static void copyAndWrite2() throws IOException {
        FileReader fileReader=new FileReader("rs/a.txt");
        FileWriter fileWriter=new FileWriter("c.txt");

        char[] bys=new char[1024];
        int len;
        while ((len=fileReader.read(bys))!=-1)
        {
            System.out.print(bys);
            fileWriter.write(bys,0,len);
        }
        fileWriter.close();
        fileReader.close();
    }

    private static void copyAndWrite3() throws IOException {
        BufferedReader bufferedReader=new BufferedReader(new FileReader("rs/a.txt"));
        BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("c.txt"));

        int bys=0;
        while ((bys=bufferedReader.read())!=-1)
        {
            System.out.print((char) bys);
            bufferedWriter.write(bys);
        }
        bufferedWriter.close();
        bufferedReader.close();
    }

    private static void copyAndWrite4() throws IOException {
        BufferedReader bufferedReader=new BufferedReader(new FileReader("rs/a.txt"));
        BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("c.txt"));

        char[] chars=new char[1024];
        int len;
        while ((len=bufferedReader.read(chars))!=-1)
        {
            System.out.print(chars);
            bufferedWriter.write(chars);
        }
        bufferedWriter.close();
        bufferedReader.close();
    }

    private static void copyAndWrite5() throws IOException {
        BufferedReader bufferedReader=new BufferedReader(new FileReader("rs/a.txt"));
        BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("c.txt"));
        String temp=null;
        while ((temp= bufferedReader.readLine())!=null)
        {
            System.out.print(temp);
            bufferedWriter.write(temp);
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
        bufferedWriter.close();
        bufferedReader.close();

    }
}
